<?php 
/**
 * File驱动
 * @author Yuwei(yuwei1122@gmail.com)
 */
class BluePhp_Library_Session_Drives_File
{	
	/**
	 * open
	 * @return unknown
	 */
	public function open()
	{
		if(!is_dir(SESS_DIR))
			mkdir(SESS_DIR,0777,true) or die('目录创建失败，请检查权限');
		return true;
	}
	
	/**
	 * close
	 * @return unknown
	 */
	public function close()
	{
		return true;
	}
	
	/**
	 * read
	 * @param unknown_type $sid
	 */
	public function read($sid)
	{
		$data = '';
		$file = SESS_DIR.'sess_'.$sid;
		if(file_exists($file) && filemtime($file) + SESS_LIFETIME > time())
			$data = file_get_contents($file);
		return $data;
	}
	
	/**
	 * write
	 * @param unknown_type $id
	 * @param unknown_type $sess_data
	 * @return unknown
	 */
	public function write($sid,$data)
	{
		$file = SESS_DIR.'sess_'.$sid;
		return file_put_contents($file, $data) === false?false:true;
	}
	
	/**
	 * destroy
	 * @param unknown_type $sid
	 */
	public function destroy($sid)
	{
		$file = SESS_DIR.'sess_'.$sid;
		if(file_exists($file)) 
			unlink($file);
		return true;
	}
	
	/**
	 * gc
	 * @return unknown
	 */
	public function gc()
	{
		foreach (glob(SESS_DIR.'sess_*') as $file) 
			if (file_exists($file) && filemtime($file) + SESS_LIFETIME < time()) 
				unlink($file);
		return true;
	}
}
?>